<?php
/*
 * author: P.L.A
 * create: 2017-09-05
 * */



namespace app\admin\controller;

use app\Admin\Model\UserModel;
use think\Db;
use think\Request;
use think\Session;
use think\model;

class User extends Base
{
    public $model = [];

    public function __initialize()
    {
        $this->model = model('user');
    }


    public function index(Request $request){
        /*接收条件*/
        $keywords_temp = $request->get();
        $order_array = [
            'id' => 'id',
            'llt' => 'last_login_time',
            'lli' => 'last_login_ip',
            'lc' => 'login_count',
            'ca' => 'create_at',
        ];
        $order = array();
        foreach ( $keywords_temp as $key=>$val){
            foreach ( $order_array as $key2=>$val2 ){
                if( $key == $key2 ){
                    $order[$order_array[$key2]] = $order[$keywords_temp[$key]];
                }
            }
        }
        $UserModel = new \app\admin\model\User();
        $page = $UserModel->paginate(20);
        $list =  $UserModel->showMember($order);
        $this->assign('list',$list);
        $this->assign('page',$page);
        return $this->fetch();
    }



    public function add(Request $request){
        //$db = db('User');
        $UserModel = new \app\admin\model\User();
        if( $request->isPost() ){
            $data = input('post.');
            $res = $UserModel->addMember($data);
            if( $res )
            {
                $this->success('插入成功','index/index');
            }else
            {
                $this->error('插入失败','index/index');
            }
        }
        return $this->fetch();
    }



    public function edit(Request $request){
        /*二话不说 先实例化类*/
        $UserModel = new \app\admin\model\User();
        /*判断请求为post请求还是ajax请求或者get请求*/
        if( $request->isAjax() ){
            $id = $request->post('id');
            /*此处需要根据具体的参数来修改*/
            $data = $request->post('data');
            /*更新结果*/
            $res = $UserModel->postEdit($id,$data);
            /*更新成功返回1  否则返回0*/
            $res = $res ? 1: 0;
            echo $res;
        }elseif( $request->isPost() ){
            /*post请求接收全部数据*/
            $id = $request->post('id');
            $data = input('post.');
            /*如果有图片上传 调用upload()方法获取文件信息*/
            if( $request->post('img') != '' ){
                $data['avatar'] = upload();
            }else{
                /*没有图片上传  就用数据库原本存储的字段覆盖表单提交的图片数据*/
                $temp = $UserModel->find($id);
                $data['avatar'] = $temp['avatar'];
            }
            $res = $UserModel->postEdit($id,$data);
            if( $res ) {
                $this->success('编辑成功', 'index/index');
            }else{
                $this->error('编辑失败', 'index/index');
            }
        }else{
            /*如果是get请求，查出信息并返回结果*/
            $id = $request->get('id');
            $vo = $UserModel->find($id);
            $this->assign('vo',$vo);
            return $this->fetch();
        }
    }

    public function test(){
        $foo = '20170830\4a4f139eab40cd82c70cea2e77713cf5.png';
        $bar = substr($foo,9);

        echo $bar;
        ///dump($bar);die;
    }

    public function Del(Request $request){
        $db = db('Banner');
        if( $request->isAjax() )
        {
            $id = $request->post('id');
            $res = $db->delete($id);
            if( $res )
            {
                echo 1;
            }else
            {
                echo 0;
            }

        }
        else
        {
            $id = $request->get('id');
            $res = $db->delete($id);
            if( $res )
            {
                $this->success('删除成功','/admin/banner/index');
            }else
            {
                $this->error('删除失败','/admin/banner/index');
            }
        }
    }




}
